package com.education.expenses.facade;

import com.education.expenses.assembler.UserProfileAssembler;
import com.education.expenses.common.dto.GridDTO;
import com.education.expenses.common.dto.UserProfileDTO;
import com.education.expenses.eao.UserEAO;
import com.education.expenses.entity.Role;
import com.education.expenses.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * User: Alex Kasianik
 * Date: 20.08.12
 * Time: 4:22
 */
@Service
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public class AdminFacade {

    @Autowired
    private UserProfileAssembler userProfileAssembler;

    @Autowired
    private UserEAO userEAO;

    public GridDTO getUserProfiles(int pageNumber, int pageCount) {

        List<User> users = userEAO.getUsersByRole(Role.APPROVER_ROLE);
        users.addAll(userEAO.getUsersByRole(Role.USER_ROLE));
        GridDTO<UserProfileDTO> gridDTO = new GridDTO<UserProfileDTO>(pageCount, pageNumber);
        gridDTO.setRecordsCount((long) users.size());
        gridDTO.setRows(userProfileAssembler.toListDTO(users));
        return gridDTO;
    }


    public void changeRole(Integer id, Role role) {
        User user = userEAO.getById(id.longValue());
        user.setRole(role);
    }

}
